package controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

	@RequestMapping("/hello.do")
	public String hello(){
		Integer.parseInt("123as");
		System.out.println("hello();");
		return "hello";
	}
		
	@RequestMapping("/hello2.do")
	public String hello2(){
		System.out.println("hello2();");

		String str="abcd";
		str.charAt(10);
		
		return "hello";
	}
	
	
	/**
	 * 这是一个异常处理方法
	 * e:处理器方法抛出的异常
	 * 
	 */
	
	
	@ExceptionHandler
	public String handleEx(Exception e,HttpServletRequest req){
		System.out.println("handleEx()");
		
		if(e instanceof NumberFormatException){
			e.printStackTrace();
			req.setAttribute("msg", "亲,请输入正确的数字");
			return "error3";
		}
		if(e instanceof StringIndexOutOfBoundsException){
			e.printStackTrace();
			req.setAttribute("msg", "下标越界");
			
			return "error3";
		}
		
		
		return "error";
		
		
	}
	
	
	
	
}














